function AdminCtrl($scope)
{
    var idForProd  = window.location.search.split('=')[1];
    $scope.CurrId = idForProd;
    $scope.enableCategory = -1;
    $scope.enableProduct = -1;
    $scope.enableTel = -1;
    $scope.enableNews = -1;
    $scope.enableAddCategory = false;
    $scope.enableSuccess = false;
    $scope.enableNewsAdd = false;
    $scope.GetNews = {};
    $scope.OtherInfo ={};
    $scope.Products = {};
    $scope.Categories = {};
    $scope.ContactInfo = {};
    $scope.slides = {};
    $scope.Users = {};
    $scope.Roles = {};

    var SWITCH_METHOD = 'http://software-site:70/php/test.php';
    var countCategory = 0;
    var countProduct = 0;


    var GetAllRoles = function(){
        $.post(SWITCH_METHOD,{method : 'getRoles'}, function (response) {
            $scope.Roles = JSON.parse(response);
            $scope.$apply();

        });
    }


    var GetAllUsers = function(){
        $.post(SWITCH_METHOD,{method : 'getUsers'}, function (response) {
            $scope.Users = JSON.parse(response);
            $scope.$apply();
        });
    };

    var GetAllCategories = function(){
        $.post(SWITCH_METHOD,{method : 'getCategories'}, function (response) {
            $scope.Categories = JSON.parse(response);
        });
    };

    var GetAllProducts = function(){

        if($scope.CurrId) {
            var id = $scope.CurrId;
            var data = {Id: id};
            $.post(SWITCH_METHOD, {method: 'GetProductByCategory', data: data}, function (response) {
                $scope.Products = JSON.parse(response);
                $scope.$apply();
            });
        }
        else {
            $.post(SWITCH_METHOD, {method: 'getProducts'}, function (response) {
                $scope.Products = JSON.parse(response);
                $scope.$apply();
            });
        }
    };
    var GetNewProducts = function(){
        $.post(SWITCH_METHOD,{method : 'getNewProducts'}, function (response) {
            $scope.NewProducts = JSON.parse(response);
            $scope.$apply();
        });
    }

    var GetOtherInfo = function(){
        $.post(SWITCH_METHOD,{method : 'getOtherInfo'}, function (response) {
            $scope.OtherInfo = JSON.parse(response);
            $scope.$apply();
        });
    };

    var GetAllNews = function(){
            $.post(SWITCH_METHOD,{method : 'GetNews'}, function (response) {
                $scope.GetNews = JSON.parse(response);
                $scope.$apply();
            });
        };

    $scope.Links = {};
    var GetAllLinks = function(){
        $.post(SWITCH_METHOD,{method : 'GetLinks'}, function (response) {
            $scope.Links = JSON.parse(response);
            $scope.$apply();

        });
        console.log($scope.Links);
    };
    GetAllLinks();
    var GetContactInfo = function(){
        $.post(SWITCH_METHOD,{method : 'GetContactInfo'}, function (response) {
            $scope.ContactInfo = JSON.parse(response);
            $scope.$apply();
        });
    };

    $scope.ShowAddCategory = function(){
        countCategory ++;
        if(countCategory == 2){
            countCategory = 0;
            $scope.enableAddCategory = false;
        }
        else{
            $scope.enableAddCategory = true;
        }
    };

    $scope.DeleteCategory = function(category){
        if(confirm("You sure?")) {

            var currId = {Id : category.Id };
            $.post(SWITCH_METHOD, {method: 'DeleteCategory', data: currId}, function (response) {
                var removeIndex = $scope.Categories.indexOf(category);
                $scope.Categories.splice(removeIndex, 1);
                $scope.$apply();
            });
        }
        else
        {

        }
    };

    $scope.editCategory = function (index) {
        $scope.enableCategory = index;
    };

    $scope.saveCategory = function (category) {
        $scope.enableCategory = -1;
        var category = {Id : category.Id, Title:category.Title};
        $.post(SWITCH_METHOD,{method : 'SaveCategory', data : category }, function (response) {
        });
    };
    $scope.saveAdv = function(adv){
        $scope.enableCategory = -1;
        var adv = {Id : adv.Id, Code:adv.Code};
        $.post(SWITCH_METHOD,{method : 'SaveAdv', data : adv }, function (response) {
        });
    }
    $scope.saveLink = function(link){
        $scope.enableNews = -1;
        var link = {
            Id : link.Id,
            Link :link.Link,
            Time : link.Time
        };
        $.post(SWITCH_METHOD,{method : 'SaveLink', data : link }, function (response) {
        });
    }

    $scope.AddCategory = function(New_Category){
        var newCat = { Title :  New_Category };
        $.post(SWITCH_METHOD,{method : 'AddCategory', data : newCat }, function (response) {
                $scope.enableAddCategory = false;
                $scope.$apply();
            $scope.enableSuccess = true;
            $scope.$apply();
            setTimeout(function(){
                $scope.enableSuccess = false;
                $scope.$apply();
                document.location.href='../../Panel/AdminPanel.php';

            },2000);
        });
    };

    $scope.ShowAddProduct = function(){
        countProduct ++;
        if(countProduct == 2){
            countProduct = 0;
            $scope.enableAddProduct = false;
        }
        else{
            $scope.enableAddProduct = true;
        }
    };



    $scope.fileNameChanged = function($event){
        var input = $event.target;
        var reader = new FileReader();
        reader.onload = function(event) {
            $scope.Image_uri = event.target.result;
            $scope.Image_name = $event.target.files[0].name;

        };
        reader.readAsDataURL(input.files[0]);
    };

    $scope.GetAdvrt = {};
    var GetAdv = function (){
        $.post(SWITCH_METHOD,{method : 'GetAdvrt'}, function (response) {
            $scope.GetAdvrt = JSON.parse(response);
            $scope.$apply();

            $('.advertising').append($scope.GetAdvrt[0].Code);
            $('.advertising_middle').append($scope.GetAdvrt[1].Code);
        });
    }

    GetAdv();






    $scope.AddUser = function(Login,Password,Select_Role) {
        var user = {
            Login: Login,
            Password : Password,
            Role : Select_Role.Title
        }
        $.post(SWITCH_METHOD, {method: 'AddUser', data: user}, function (response) {
            console.log(response);
            $scope.enableAddProduct = false;
            $scope.$apply();
            $scope.enableSuccess = true;
            $scope.$apply();
            setTimeout(function(){
                $scope.enableSuccess = false;
                $scope.$apply();
                document.location.href='../../Panel/UserPanel.php';
            },2000);
        });

    }
    $scope.DeleteUser = function(user){
        if(confirm("You sure?")) {

            var currId = {Id : user.Id };
            $.post(SWITCH_METHOD, {method: 'DeleteUser', data: currId}, function (response) {
                var removeIndex = $scope.Users.indexOf(user);
                $scope.Users.splice(removeIndex, 1);
                $scope.$apply();
            });
        }
        else
        {

        }
    };
    $scope.saveUser = function(user){
        $scope.enableCategory = -1;
        var user = {
            Id : user.Id,
            Login : user.Login,
            Password : user.Password
        };
        $.post(SWITCH_METHOD,{method : 'SaveUser', data : user }, function (response) {
        });
    }

    $scope.AddProduct = function(Product_title,Download_link,Product_file_size,Product_Requirements,Product_Languages,Product_Date,Product_Author,Home_page,Product_Description_preview,Product_Description,Select_Category) {
        var product = {
            Title: Product_title,
            Download_link : Download_link,
            Image : $scope.Image_uri,
            Image_name: $scope.Image_name,
            Home : Home_page,
            File_size: Product_file_size,
            Requirements: Product_Requirements,
            Languages: Product_Languages,
            Date: Product_Date,
            Author: Product_Author,
            Description_preview: Product_Description_preview,
            Description: Product_Description,
            Category_Name: Select_Category.Title

        };
        $.post(SWITCH_METHOD, {method: 'AddProduct', data: product}, function (response) {
           $scope.enableAddProduct = false;
            $scope.$apply();
            $scope.enableSuccess = true;
            $scope.$apply();
            setTimeout(function(){
                $scope.enableSuccess = false;
                $scope.$apply();
                document.location.href='../../Panel/ProductPanel.php';
            },2000);
        });
    };

    $scope.DeleteProduct = function(product){
        if(confirm("You sure?")) {

            var currId = {Id : product.Id };
            $.post(SWITCH_METHOD, {method: 'DeleteProduct', data: currId}, function (response) {
                var removeIndex = $scope.Products.indexOf(product);
                $scope.Products.splice(removeIndex, 1);
                $scope.$apply();
            });
        }
        else
        {

        }
    };

    $scope.editProduct = function(index){
        $scope.enableProduct = index;
    };

    $scope.saveProduct = function(product){
        $scope.enableProduct = -1;
        var product = {
            ProductId : product.Id,
            Title: product.Title,
            Download_link : product.Download_link,
            Image : product.Image,
            Home : product.Homepage,
            File_size: product.File_size,
            Requirements: product.Requirements,
            Languages: product.Languages,
            Date: product.Date_added,
            Author: product.Author,
            Description_preview: product.Description_preview,
            Description: product.Description,
            CategoryId : product.CategoryId
        };
        $.post(SWITCH_METHOD,{method : 'SaveProduct', data : product }, function (response) {
        });

    };

    $scope.editTelephone = function(index){
        $scope.enableTel = index;
    };

    $scope.saveTelephone = function(telephone){
        $scope.enableTel = -1;
        var info = {
            Id : telephone.Id,
            Tel: telephone.Telephone
        };
        $.post(SWITCH_METHOD,{method : 'SaveTelephone', data : info }, function (response) {
        });
    };

    $scope.ShowAddNews = function(){
        countProduct ++;
        if(countProduct == 2){
            countProduct = 0;
            $scope.enableNewsAdd = false;
        }
        else{
            $scope.enableNewsAdd = true;
        }
    };

    $scope.AddNews = function(News_title,News_Date,News_Description)
    {
        var News = {
            Title : News_title,
            Date : News_Date,
            Description: News_Description
        };
        $.post(SWITCH_METHOD,{method : 'AddNews', data : News }, function (response) {
            $scope.enableNewsAdd = false
            $scope.$apply();
            $scope.enableSuccess = true;
            $scope.$apply();

            setTimeout(function(){
                $scope.enableSuccess = false;
                $scope.$apply();
                document.location.href='../../Panel/NewsPanel.php';
            },2000);
        });
    };

    $scope.editNews = function(index){
        $scope.enableNews = index;
    };

    $scope.saveNews = function(news){
        $scope.enableNews = -1;
        var news = {
            Id : news.Id,
            Title : news.Title,
            Date : news.Date,
            Description : news.Description
        };
        $.post(SWITCH_METHOD,{method : 'SaveNews', data : news }, function (response) {

        });
    };

    $scope.DeleteNews = function(news){
        if(confirm("You sure?")) {

            var currId = {Id : news.Id };
            $.post(SWITCH_METHOD, {method: 'DeleteNews', data: currId}, function (response) {
                var removeIndex = $scope.GetNews.indexOf(news);
                $scope.GetNews.splice(removeIndex, 1);
                $scope.$apply();
            });
        }
        else
        {

        }
    };

    $scope.AddContactInfo = function(userName,userEmail,userCompany,userMsg)
    {
        var info = {
            UserName : userName,
            Email : userEmail,
            Company : userCompany,
            Msg : userMsg
        };
        $.post(SWITCH_METHOD, {method: 'AddContactInfo', data: info}, function (response) {

            if(response){
               document.location.href='../../index.html';
            }
        });
    };

    $scope.DeleteMsg = function(msg){
        var CurrId = { Id : msg.Id};
        if(confirm("You sure?")) {

            $.post(SWITCH_METHOD, {method: 'DeleteMsg', data: CurrId}, function (response) {
                var removeIndex = $scope.ContactInfo.indexOf(msg);
                $scope.ContactInfo.splice(removeIndex, 1);
                $scope.$apply();
            });
        }
        else
        {

        }
    };
    var getSlides = function()
    {
        $.post(SWITCH_METHOD,{method : 'getSlides'}, function (response) {
            $scope.slides = JSON.parse(response);
            $scope.$apply();
        });

    };
    $scope.init = function()
    {
        GetContactInfo();
        GetAllNews();
        GetAllCategories();
        GetAllProducts();
        GetOtherInfo();
        getSlides();
        GetNewProducts();
        GetAllUsers();
        GetAllRoles();
    };

    $scope.addToSlider = function (product) {
        $.post(SWITCH_METHOD, {method : 'addToSlider', productId: product.Id}, function (response) {

        })
    };

    $scope.removeSlide = function(slide)
    {
        $.post(SWITCH_METHOD,{method : 'removeSlide', slideId : slide.ProductId }, function(response){
            if(response != null || response != undefined)
            {
                var index = $scope.slides.indexOf(slide);
                $scope.slides.splice(index,1);
                $scope.$apply();
            }
        });
    };


    $scope.init();

}